 <template>
 	<view class="container bg-color-gray-light">

 		<view class="w100 bg-color-white p30 mt20">
 			<view class="color-danger f28 lh40">
 				线上只能对常见病、慢病进行复诊，如没有线下就诊过，请先前往线下医院就诊
 			</view>
 		</view>
 		<view class="w100 bg-color-white p30 mt20 pb15" v-if="biz == 'RX'">
 			<view class="flex_lr_m">
 				<view class="f32 b c333 pb30">药品<text class="rd">*</text></view>
 				<view class="f32 b color-primary pb30" @click="onAddDrug">+添加药品</view>
 			</view>
 			<view>
 				<!-- a{{drugArr.length}}b -->
 				<view v-for="(item,index) in drugArr" :key="index">
 					<view class="goods-info" style="margin-bottom: 20rpx;">
 						<view class="flex_lr_m">
 							<view class="goods-text-info-box">

 								<text class="c333 f32"
 									v-if="item.productName">{{item.productName}}{{item.rxType}}</text>
 								<text class="c333 f32" v-if="item.name">{{item.name}}{{item.rxType}}</text>
 								<text class="goods-info-item" style="color: #999;">{{item.spec}}</text>
 							</view>
 							<view>
 								<view class="delete-btn" @click="onDelete(index)">
 									<uni-icons class="selected-icon" type="trash" size="50rpx"
 										style="color: #0041ef;"></uni-icons>
 								</view>
 							</view>
 						</view>
 						<view class="goods-price-box">
 							<view class="basic-footer">
 								<text class="goods-price" style="color: #f00;">￥{{item.salePrice}}</text>
 								<view class="cart-num">
 									<view class="symbol" @click="handleOperateCount(item,index,1)">-</view>
 									<view class="count">
 										{{item.quantity}}
 									</view>
 									<view class="symbol" @click="handleOperateCount(item,index,2)">+</view>
 								</view>
 							</view>
 						</view>
 					</view>



 				</view>
 			</view>
 		</view>
 		<view class="w100 bg-color-white p30 mt20 pb15">
 			<view style="display: flex;justify-content: space-between;">
 				<view class="f32 b c333 pb30">过敏史<text class="rd">*</text>

 				</view>
 				<radio-group class="radio" @change="onLiverFunctionRadioChange1">
 					<view class="dib mr60 chooseItem">
 						<radio value="NORMAL" :checked="allergy == 'NORMAL'" color="#0041ef">
 							<text class="f32 ml10 c333">是</text>
 						</radio>
 					</view>
 					<view class="dib chooseItem">
 						<radio value="ABNORMAL" :checked="allergy == 'ABNORMAL'" color="#0041ef">
 							<text class="f32 ml10 c333">否</text>
 						</radio>
 					</view>
 				</radio-group>

 			</view>
 			<textarea placeholder="请输入您的过敏史..." v-if="allergy == 'NORMAL'" v-model="allergyConfirm" @input="onIllnessTextChange1"
 				placeholder-style="c999"></textarea>
 			<view class="drug pt30"  v-if="allergy == 'NORMAL'">
 				<text v-for="(item,index) in tag1" :key="index" @click="onIllnessTagChoose1(item)">{{item}}</text>
 			</view>
 		</view>
 		<view class="w100 bg-color-white p30 mt20 pb15">
 			<view style="display: flex;justify-content: space-between;">
 				<view class="f32 b c333 pb30">既往史<text class="rd">*</text>

 				</view>
 				<radio-group class="radio" @change="onRenalFunctionRadioChange2">
 					<view class="dib mr60 chooseItem">
 						<radio value="NORMAL" :checked="pastHistory == 'NORMAL'" color="#0041ef">
 							<text class="f32 ml10 c333">是</text>
 						</radio>
 					</view>
 					<view class="dib chooseItem">
 						<radio value="ABNORMAL" :checked="pastHistory == 'ABNORMAL'" color="#0041ef">
 							<text class="f32 ml10 c333">否</text>
 						</radio>
 					</view>
 				</radio-group>

 			</view>

 			<textarea placeholder="请输入您的既往史..." v-if="pastHistory == 'NORMAL'" v-model="pastHistoryConfirm" @input="onIllnessTextChange2"
 				placeholder-style="c999"></textarea>
 			<view class="drug pt30" v-if="pastHistory == 'NORMAL'" >
 				<text v-for="(item,index) in tag2" :key="index" @click="onIllnessTagChoose2(item)">{{item}}</text>
 			</view>
 		</view>
 		<view class="w100 bg-color-white p30 mt20 pb15">
 			<view class="f32 b c333 pb30">线下诊断<text class="rd">*</text></view>
 			<textarea placeholder="请输入您的线下诊断..." v-model="illnes" @input="onIllnessTextChange"
 				placeholder-style="c999"></textarea>
 			<view class="drug pt30">
 				<text v-for="(item,index) in tag" :key="index" @click="onIllnessTagChoose(item)">{{item}}</text>
 			</view>
 		</view>
 		<view class="w100 bg-color-white p30 mt20 pb15">
 			<view class="f32 b c333 pb30">病情描述<text class="rd">*</text></view>
 			<textarea placeholder="请描述病情症状..." :value="info.symptom" @input="onSymptomInput"
 				placeholder-style="c999"></textarea>
 		</view>

 		<view class="w100 bg-color-white p30 mt20">
 			<view class="f32 b c333 mt20">肝功能是否正常？<text class="rd">*</text></view>
 			<view class="clearfix pt30">
 				<radio-group class="radio" @change="onLiverFunctionRadioChange">
 					<view class="dib mr60 chooseItem">
 						<radio value="NORMAL" :checked="info.liverFunction == 'NORMAL'" color="#0041ef">
 							<text class="f32 ml10 c333">是</text>
 						</radio>
 					</view>
 					<view class="dib chooseItem">
 						<radio value="ABNORMAL" :checked="info.liverFunction == 'ABNORMAL'" color="#0041ef">
 							<text class="f32 ml10 c333">否</text>
 						</radio>
 					</view>
 				</radio-group>
 			</view>
 		</view>
 		<view class="w100 bg-color-white p30 mt20">
 			<view class="f32 b c333 mt20">肾功能是否正常？<text class="rd">*</text></view>
 			<view class="clearfix pt30">
 				<radio-group class="radio" @change="onRenalFunctionRadioChange">
 					<view class="dib mr60 chooseItem">
 						<radio value="NORMAL" :checked="info.renalFunction == 'NORMAL'" color="#0041ef">
 							<text class="f32 ml10 c333">是</text>
 						</radio>
 					</view>
 					<view class="dib  chooseItem">
 						<radio value="ABNORMAL" :checked="info.renalFunction == 'ABNORMAL'" color="#0041ef">
 							<text class="f32 ml10 c333">否</text>
 						</radio>
 					</view>
 				</radio-group>
 			</view>
 		</view>

 		<view class="flex p30 agreement">
 			<view class="w100 flex">

 				<checkbox-group @change="radioChange">
 					<label style="display: flex;align-items: center;flex-wrap: wrap;">
 						<checkbox style="transform:scale(0.7)" value="cb" checked="true" />
 						<text class="c666 f24">我已阅读并同意</text>
 						<text class="f24 color-primary" @click.stop="onTapAgreement(11)">《用户协议》</text>
 						<text class="f24 color-primary" @click.stop="onTapAgreement(12)">《隐私政策》</text>
 						<text class="f24 color-primary" style="margin-left: 50rpx;"
 							@click.stop="onTapAgreement(6)">《互联网问诊知情同意书》</text>

 					</label>

 				</checkbox-group>
 				<!-- <van-checkbox value="{{ isAgreementChecked }}" bind:change="onIsAgreementCheckedChange"
					data-type="checked" icon-size="28rpx" custom-class="dib" checked-color="#0041ef">
				</van-checkbox> -->
 			</view>
 		</view>
 		<view class="fixed b0 l0 w100 bg-color-white pl30 pr30 confir">
 			<button class="submit-btn" @click="onSubmit">提交</button>
 		</view>
 	</view>

 </template>

 <script>
 	import {
 		qzApi
 	} from '@/config/request.js';
 	export default {
 		data() {
 			return {
 				isTy: 1,
 				tag: ['上呼吸道感染', '慢性支气管炎', '急性胃炎', '肝硬化', '糖尿病', '高血压'],
 				tag1: ['青霉素过敏', '磺胺过敏', '泛影葡胺过敏', '地卡因过敏', '链霉素过敏'],
 				tag2: ['高血压', '糖尿病', '冠心病', '脑血管病', '肾病', '慢性肝炎', '吸烟', '酗酒', '未初潮', '已绝经', '花粉过敏', '霉菌过敏', ],

 				isAgreementChecked: 1,
 				advisoryIdKey: '',
 				apigw: "TRQA",
 				applyId: '',
 				biz: '', // 业务类型
 				drugArr: [],
 				illnes: "",
				pastHistoryConfirm:"",
				allergyConfirm:"",
 				info: {
 					"applyType": 2,
 					"costType": 0,
 					"illness": "",
 					"orderId": "",
 					"liverFunction": "",
 					"prescriptionType": 0,
 					"renalFunction": "",
 					"symptom": "",
 					"claimNumber": "", // 东璧后报销流程中，从东璧小程序跳转时传过来的理赔ID
 					"allergy": "",
					"pastHistory": "",
 					
 				},
 				isNeedRedirect: false, // 
				"allergy": "",
				"pastHistory": "",
 			}
 		},
 		onLoad(options) {
			console.log(options,"kddkkd")
 			if (options.orderId) {
 				this.info.orderId = options.orderId
 			}
 		},
 		onShow() {
 			this.biz = uni.getStorageSync('biz');
 			console.log(this.biz)
 			if (this.isNeedRedirect) {
 				if (this.biz == 'RX') {
 					this.wxNavigateToMiniProgram() // 跳转到东璧小程序
 				} else {
 					this.gotoOrderDetail()
 				}
 			}

 		},

 		methods: {
 			wxNavigateToMiniProgram() {
 				let userAccounts = uni.getStorageSync('DB_USER_ACCOUNTS')
 				let tokenKey = uni.getStorageSync('DB_TOKEN_KEY')
 				let claimNumber = uni.getStorageSync('DB_CLAIM_NUMBER')
 				uni.navigateToMiniProgram({
 					appId: 'wxfdb0048182641ba6',
 					path: `pages/lipei/home/sucess?userAccounts=${userAccounts}&tokenkey=${tokenKey}&claimNumber=${claimNumber}`,
 					envVersion: 'trial',
 					success(res) {
 						// 打开成功
 					}
 				})
 			},
 			handleOperateCount(item, index, type) {
 				console.log(index)
 				if (type == 1) {

 					if (this.drugArr[index].quantity > 1) {
 						this.drugArr[index].quantity--
 					}
 				} else {
 					this.drugArr[index].quantity++

 					console.log(this.drugArr[index].quantity)
 				}
 				this.$forceUpdate()
 			},
 			onDelete(index) {
 				this.drugArr.splice(index, 1)
 			},

 			async gotoOrderDetail() {
				
 				uni.redirectTo({
 					url: '/page1/order/index?id=' + this.info.orderId,
 				})
 			},

 			radioChange(e) {
 				console.log(e)
 				if (e.detail.value.length <= 0) {
 					this.isAgreementChecked = 0
 				} else {
 					this.isAgreementChecked = 1
 				}
 			},
 			onTapAgreement(type) {
 				uni.navigateTo({
 					url: '/page1/webView/webView?webType=AGREEMENT&articleType=' + type
 				})
 			},
 			onIllnessTextChange(e) {
 				this.info.illness = e.detail.value ? this.$check.filterEmoji(e.detail.value) : e.detail.value;
 			},
			onIllnessTextChange1(e) {
				this.info.allergyConfirm = e.detail.value ? this.$check.filterEmoji(e.detail.value) : e.detail.value;
			},
			onIllnessTextChange2(e) {
				this.info.pastHistoryConfirm = e.detail.value ? this.$check.filterEmoji(e.detail.value) : e.detail.value;
			},
 			onSymptomInput(e) {
 				this.info.symptom = e.detail.value ? this.$check.filterEmoji(e.detail.value) : e.detail.value;
 			},
 			onIllnessTagChoose(e) {
 				var value = e;
 				var text = this.illnes;
 				this.illnes = text == '' ? value : text + ',' + value;
 				console.log(text)
 			},
			onIllnessTagChoose1(e) {
				var value = e;
				var text = this.allergyConfirm;
				this.allergyConfirm = text == '' ? value : text + ',' + value;
				console.log(text)
			},
			onIllnessTagChoose2(e) {
				var value = e;
				var text = this.pastHistoryConfirm;
				this.pastHistoryConfirm = text == '' ? value : text + ',' + value;
				console.log(text)
			},
 			// 肝功能是否正常
 			onLiverFunctionRadioChange(e) {
 				this.info.liverFunction = e.detail.value
 				console.log(this.info)
 			},
 			// 肾功能是否正常
 			onRenalFunctionRadioChange(e) {
 				this.info.renalFunction = e.detail.value
 			},
 			// 肝功能是否正常
 			onLiverFunctionRadioChange1(e) {
 				this.allergy = e.detail.value
 				console.log(this.info)
 			},
 			// 肾功能是否正常
 			onRenalFunctionRadioChange2(e) {
 				this.pastHistory = e.detail.value
 			},

 			onSubmit() {
 				this.info.illness = this.illnes;
				
 				if (this.validForm()) {
 					if (this.biz == 'RX') {
 						this.handleSkuIdMaps()
 					}
 					console.log(this.info)
					
					this.info.allergy = this.allergyConfirm;
					this.info.pastHistory = this.pastHistoryConfirm;
					
 					this.applyAdvisoryRequest(this.info)
 				}
 			},
 			handleSkuIdMaps() {
 				let map = {}
 				this.drugArr.forEach(element => {
 					console.log(element);
 					map[element.id] = element.quantity
 				});
 				this.info.skuIdMaps = map
 			},



 			async applyAdvisoryRequest(params) {
 				console.log('applyAdvisoryRequest params', params);
 				try {
 					// wx.showLoading()
 					let applyData = await qzApi.applyAdvisoryRequest(params)
					
					if(applyData.code==-1){
						uni.showToast({
							'title': applyData.msg,
							icon: 'none'
						})
					}else if(!applyData.data){
						uni.showToast({
							'title': applyData.msg,
							icon: 'none'
						})
					}else{
						this.advisoryIdKey = applyData.data.advisoryIdKey
						this.applyId = applyData.data.applyId
						this.gotoConsultationPage()
					}
					
 					

 				} catch (error) {
 					console.log(error);
 				} finally {
 					// wx.hideLoading()
 				}
 			},
 			gotoConsultationPage() {
 				// console.log('gotoConsultationPage advisoryIdKey', this.data.advisoryIdKey);
 				// console.log('gotoConsultationPage applyId', this.data.applyId);
 				// console.log('gotoConsultationPage apigw', this.data.apigw);

 				let url =
 					`/page1/zyim/index?advisoryIdKey=${this.advisoryIdKey}&apigw=${this.apigw}&applyId=${this.applyId}&biz=${this.biz}`
 				this.isNeedRedirect = true
 				console.log(url)
 				uni.navigateTo({
 					// url: `/pages/consultation/consultation?advisoryIdKey=${advisoryIdKey}&apigw=${this.data.apigw}&applyId=${applyId}`
 					url: url
 				})
 			},
 			validForm() {
				
 				if (this.biz == 'RX' && this.drugArr.length == 0) {
 					uni.showToast({
 						title: '请添加药品',
 						icon: 'none'
 					})
 					return false
 				}
				if (this.$check.isValidNull(this.allergy)) {
					uni.showToast({
						'title': '请选择是否有过敏史',
						icon: 'none'
					})
					return false
				}
				if (this.allergy=="NORMAL"&&!this.allergyConfirm) {
					uni.showToast({
						'title': '请输入过敏史',
						icon: 'none'
					})
					return false
				}
				
				if (this.$check.isValidNull(this.pastHistory)) {
					uni.showToast({
						'title': '请选择是否有既往史',
						icon: 'none'
					})
					return false
				}
				if (this.pastHistory=="NORMAL"&&!this.pastHistoryConfirm) {
					uni.showToast({
						'title': '请输入既往史',
						icon: 'none'
					})
					return false
				}
				
				
 				if (this.$check.isValidNull(this.info.illness)) {
 					uni.showToast({
 						title: '请输入线下诊断',
 						icon: 'none'
 					})
 					return false
 				} else if (this.$check.isValidNull(this.info.symptom)) {
 					uni.showToast({
 						'title': '请输入病情描述',
 						icon: 'none'
 					})
 					return false
 				} else if (this.$check.isValidNull(this.info.liverFunction)) {
 					uni.showToast({
 						'title': '请选择肝功能是否正常',
 						icon: 'none'
 					})
 					return false
 				} else if (this.$check.isValidNull(this.info.renalFunction)) {
 					uni.showToast({
 						'title': '请选择肾功能是否正常',
 						icon: 'none'
 					})
 					return false
 				} else if (this.isAgreementChecked != 1) {
 					uni.showToast({
 						title: '请阅读并同意《互联网问诊知情同意书》',
 						icon: 'none'
 					})
 					return false
 				}
 				return true
 			},
 			onAddDrug() {
 				uni.navigateTo({
 					url: '/page1/searchDrug/searchDrug'
 				})
 			}
 		},

 	}
 </script>

 <style scoped lang="scss">
 	/* pages/consultationInfo/consultationInfo.wxss */
 	.container {
 		padding-bottom: 224rpx;
 	}

 	textarea {
 		width: 100%;
 		height: 200rpx;
 		/* border: 2rpx solid #EEEEEE; */
 		box-sizing: border-box;
 		padding: 20rpx;
 		border-radius: 8rpx;
 		color: #333;
 		font-size: 32rpx;
 		background-color: #f8f8f8;
 	}

 	.drug text {
 		height: 56rpx;
 		box-sizing: border-box;
 		border: 2rpx solid #EEEEEE;
 		border-radius: 4rpx;
 		display: inline-block;
 		font-size: 28rpx;
 		color: #999999;
 		padding: 0 16rpx;
 		line-height: 52rpx;
 		margin-right: 20rpx;
 		margin-bottom: 20rpx;
 	}

 	.drug text.cur {
 		background: var(--themeColor);
 		border-color: var(--themeColor);
 		color: #fff;
 	}

 	.upload {
 		float: left;
 		width: 140rpx;
 		height: 140rpx;
 		margin-top: 20rpx;
 	}

 	.upload image {
 		display: block;
 		width: 100%;
 		height: 100%;
 	}

 	.picList .item {
 		display: block;
 		float: left;
 		margin-right: 40rpx;
 		margin-top: 20rpx;
 		width: 140rpx;
 		height: 140rpx;
 		position: relative;
 		overflow: visible;
 	}

 	.picList .item .delPic {
 		width: 40rpx;
 		height: 40rpx;
 		position: absolute;
 		right: -20rpx;
 		top: -20rpx;
 	}

 	.picList .item .delPic image {
 		display: block;
 		width: 100%;
 		height: 100%;
 	}

 	.picList .item>image {
 		display: block;
 		width: 100%;
 		height: 100%;
 	}

 	.picList .item:nth-child(4n) {
 		margin-right: 0rpx;
 	}

 	.confir button.gray {
 		background: #ddd;
 	}

 	.noIcon {
 		width: 44rpx;
 		height: 44rpx;
 		position: absolute;
 		right: 0;
 		top: 50%;
 		margin-top: -22rpx;
 	}

 	.lh44 {
 		line-height: 44rpx;
 	}

 	.tag {
 		background: #E7F4FF;
 		border-radius: 4rpx;
 		width: 76rpx;
 		height: 36rpx;
 		text-align: center;
 		display: inline-block;
 		vertical-align: middle;
 		color: var(--themeColor);
 	}

 	.rd {
 		margin-left: 8rpx;
 		color: #ff5555;
 	}

 	.agreement .van-checkbox {
 		display: -webkit-flex;
 		display: flex;
 		align-items: unset;
 		overflow: hidden;
 		-webkit-user-select: none;
 		user-select: none;
 		position: relative;
 	}

 	.agreement .van-checkbox .van-checkbox__icon {
 		margin-top: 10rpx;
 	}


 	.submit-btn {
 		height: 80rpx;
 		line-height: 80rpx;
 	}

 	.delete-btn {}

 	.head {
 		width: 100%;
 		position: relative;
 		height: 168rpx;
 	}

 	.head .content {
 		position: absolute;
 		left: 0;
 		top: 0;
 		width: 100%;
 		height: 100%;
 		line-height: 168rpx;
 		font-size: 32rpx;
 		color: #fff;
 		font-weight: bold;
 	}

 	.head .content image {
 		display: inline-block;
 		width: 44rpx;
 		height: 44rpx;
 		vertical-align: top;
 		margin-top: 62rpx;
 		margin-right: 10rpx;
 	}

 	.head .content text {
 		font-weight: normal;
 		font-size: 26rpx;
 		padding-left: 40rpx;
 	}

 	.consignee {
 		background: #fff;
 	}

 	.consignee .MapIcon {
 		width: 44rpx;
 		height: 44rpx;
 	}

 	.title {
 		height: 92rpx;
 		line-height: 92rpx;
 	}

 	.img image {
 		width: 100rpx;
 		height: 100rpx;
 		border: 1rpx solid #eee;
 		border-radius: 8rpx;
 	}

 	.confir .fixed_btn {
 		width: 170rpx;
 		height: 56rpx;
 		border-radius: 8rpx;
 		font-size: 26rpx;
 	}

 	.confir .fixed_btn:nth-child(1) {
 		color: #666;
 		border: 2rpx solid #E4E4E4;
 		background: #fff;
 	}

 	.confir .fixed_btn:nth-child(2) {
 		color: #fff;
 		background: #367DFF;
 		margin-left: 30rpx;
 	}

 	.pbbtn {
 		padding-bottom: 150rpx;
 	}

 	.cancel-btn {
 		border: 2rpx solid #E4E4E4;
 		color: #666;
 	}

 	.logistics-btn {
 		border: 2rpx solid var(--themeColor);
 		color: var(--themeColor);
 	}

 	.confirm-btn {
 		background: var(--themeColor);
 		border: 2rpx solid var(--themeColor);
 		color: #fff;
 	}

 	.pay-btn {
 		background: var(--btnDangerColor);
 		border: 2rpx solid var(--btnDangerColor);
 		color: #fff;
 	}

 	.operation-btn {
 		height: 80rpx;
 		width: 200rpx;
 		border-radius: 40rpx;
 		font-size: 26rpx;
 	}

 	.fresh-btn {
 		color: var(--themeColor);
 	}

 	.basic-footer {
 		display: flex;
 		justify-content: space-between;

 		.cart-num {
 			display: flex;

 			.count {
 				width: var(--stepper-input-width, 32px);
 				height: var(--stepper-input-height, 28px);
 				background-color: var(--stepper-background-color, #f2f3f5);
 				text-align: center;
 				margin: 0 5rpx;
 				color: var(--stepper-input-text-color, #323233);
 				font-size: var(--stepper-input-font-size, 14px);
 				line-height: var(--stepper-input-height, 28px);

 			}

 			.symbol {
 				display: flex;
 				align-items: center;
 				justify-content: center;
 				width: var(--stepper-input-height, 28px);
 				height: var(--stepper-input-height, 28px);
 				background-color: var(--stepper-background-color, #f2f3f5);
 				font-size: 40rpx;
 			}
 		}
 	}
 </style>